// pages/http/http.js
// const http = require("../../models/http.js")
Page({

  handleSubmit(event) {
    var keyword = event.detail.value.keyword;
    wx.request({
      url: `http://192.168.4.18:3000/search?keywords=${keyword}&type=10`,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        var albums = res.data.result.albums;
        console.log(albums)
        var musics = []
        albums.forEach(item => {
          var obj = {};
          if(item.name.length>10){
            item.name = item.name.slice(0,10)+"..."
          }

          obj.name = item.name;
          obj.id = item.id;
          if(item.artist.name.length>7){
            item.artist.name = item.artist.name.slice(0,7)+"..."
          }
          obj.artistName = item.artist.name;
          obj.picUrl = item.picUrl;
          musics.push(obj)
        })
        this.setData({
          musics
        })
      }
    });
  },

  /**
   * 页面的初始数据
   */
  data: {
    albums:"",
    /* 记录已经播放的id */
    prevID:"",
    // isPlay:false,
    /* 存所有播放的id */
    playStates:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad: function (options) {
  //   http({
  //     keyword:"你",
  //     success: res=> {
  //       console.log(res);
  //       let albums = res.data.result.albums
  //       this.setData({
  //         albums
  //       })
  //     }
  //   })
  // },

  handleClick(event){
    console.log(event.currentTarget.dataset);

    let id = event.currentTarget.dataset.id;
    let playStates = this.data.playStates;
    /* 当前项目id和点击之后id值不同的情况 */
    if(id !=this.data.prevID){
      playStates[id] = true;

          for(let key in playStates){
            if(key !=id){
              playStates[key] = false;
            }
            /* if(key ==id){
              playStates[key] = true;
            }else{
              playStates[key] = false
            } */
          }

          this.setData({
            playStates,
            prevID:id
          })

    }else{ /*当前项目id和点击之后的id值相同的情况  */
      
      playStates[id]= !playStates[id];
        this.setData({
          playStates
        })
    }
    console.log(playStates); 
  }
})



